<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var compress = function (chars) {
      let len = chars.length;
      let sum = 1;
      let add = 0;
      for (let j = 0; j < len; j++) {
        if (chars[j + 1] == chars[j]) {
          sum++;
        } else {
          let str = String(sum);
          if (str > 1) {
            chars[add++] = chars[j];
            for (let i of str) chars[add++] = i;
          } else {
            chars[add++] = chars[j];
            if (sum !== 1) chars[add++] = str;
          }
          sum = 1;
        }
      }
      return add
    };
    console.log(compress(["a", "a", "b", "b", "c", "c", "c"]));


  </script>
</body>

</html>